package com.hyts.serialize.execute;

import com.hyts.serialize.config.JsonType;
import com.hyts.serialize.json.JsonSerializeHandler;
import com.hyts.serialize.json.fastjson.FastJsonSerializeHandler;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.serialize.execute
 * author:Libo/Alex
 * create-date:2019-06-28 11:09
 * email:liboware@gmail.com
 * description:此类主要用于: json序列化操作服务
 */
public class JsonExecutor<T> {


    JsonSerializeHandler<T> jsonSerializeHandler;


    private JsonExecutor(){
        this((JsonSerializeHandler<T>) new FastJsonSerializeHandler());
    }


    private JsonExecutor(JsonSerializeHandler<T> jsonSerializeHandler) {
        this.jsonSerializeHandler = jsonSerializeHandler;
    }

    /**
     * 转成json字符串
     * @param param
     * @return
     */
    public String toJSONString(Object param){
        return jsonSerializeHandler.buildJSONString(param);
    }

    /**
     *
     * @param jsonStr
     * @param clazz
     * @return
     */
    public T toJavaBean(String jsonStr,Class<T> clazz){
        return jsonSerializeHandler.buildBean(jsonStr,clazz);
    }



    /**
     * 构建起
     * @return
     */
    public static JsonExecutor build(){
        return new JsonExecutor();
    }

    /**
     * 构建起
     * @param jsonSerializeHandler
     * @return
     */
    public static JsonExecutor build(JsonSerializeHandler jsonSerializeHandler){
        return new JsonExecutor(jsonSerializeHandler);
    }
}
